package com.jv.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jv.entity.BigType;
import com.jv.entity.Product;
import com.jv.entity.R;
import com.jv.entity.SmallType;
import com.jv.service.IBigTypeService;
import com.jv.service.IProductService;
import com.jv.service.ISmallTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

/**
 * 商品大类Controller
 * @author Jv
 * @version 1.0
 */
@RestController
@RequestMapping("/bigType")
public class BigTypeController {
    @Autowired
    private IBigTypeService bigTypeService;
    @Autowired
    private ISmallTypeService smallTypeService;
    @Autowired
    private IProductService productService;


    @GetMapping("/findAll")//查询所有商品大类
    public R findSwiper(){
        List<BigType> bigTypeList = bigTypeService.list();
        HashMap<String, Object> map = new HashMap<>();
        map.put("message",bigTypeList);
        return R.ok(map);
    }
    //获取所有菜单信息
    @GetMapping("/findCategories")
    public R findCategories(){
        List<BigType> bigTypeList = bigTypeService.list();
        for (BigType bigType : bigTypeList) {
            List<SmallType> smallTypeList = smallTypeService.list(new QueryWrapper<SmallType>().eq("bigTypeId", bigType.getId()).orderByAsc("name"));
            bigType.setSmallTypeList(smallTypeList);
            for (SmallType smallType : smallTypeList) {
                List<Product> productList = productService.list(new QueryWrapper<Product>().eq("typeId", smallType.getId()));
                smallType.setProductList(productList);
            }
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("message",bigTypeList);
        return R.ok(map);
    }
}
